import { IPSAppDataEntity, IPSAppDEField, IPSAppDEUIAction } from '@ibiz/dynamic-model-api';
import { GlobalService, LogUtil } from '@ibizstudio/api';
import { UIServiceHooks } from '../../hooks';
import { UIServiceBase } from './ui-base.service';

/**
 * UI 服务
 *
 * @author chitanda
 * @date 2022-05-17 18:05:59
 * @export
 * @class UIService
 * @extends {UIServiceBase}
 */
export class UIService extends UIServiceBase {
    constructor(model: IPSAppDataEntity, context: IContext) {
        super(context);
        this.entityModel = model;
    }

    protected async loaded() {
        await super.loaded();
        this.dataService = await new GlobalService().getService(this.entityModel.codeName);
    }

    protected async initActionMap(): Promise<void> {
        const actions = this.entityModel?.getAllPSAppDEUIActions() as IPSAppDEUIAction[];
        if (actions && actions.length > 0) {
            for (const action of actions) {
                const ctx = { result: null };
                await UIServiceHooks.initLogic.call(ctx, action, this.context);
                if (!ctx.result) {
                    LogUtil.warn('未实例化行为：', action);
                }
                this.actionMap.set(action.uIActionTag, ctx.result);
            }
        }
    }

    protected initBasicData(){
      this.isEnableDEMainState = this.entityModel.enableDEMainState;
      this.dynaInstTag = this.entityModel.dynaInstTag;
      this.tempOrgIdDEField = this.entityModel.getOrgIdPSAppDEField()?.codeName || null;
      this.multiFormDEField = this.entityModel.getFormTypePSAppDEField()?.codeName || null;
      this.indexTypeDEField = this.entityModel.getIndexTypePSAppDEField()?.codeName || null;
      this.mainStateFields = [];
      this.entityModel.getMainStatePSAppDEFields()?.forEach((field: IPSAppDEField) => {
        this.mainStateFields.push(field.codeName);
      })
    }
}
